using System;
using System.Collections.Generic;
using System.Text;

namespace TmxLib
{
    public class TranslationExporter
    {

        public class Args
        {
            public string ProjectName;
            public TmxImage Original;
            public ExcludeContextCollection ExcludeList = new ExcludeContextCollection();
            public TmxLanguageCollection RequiredLanguages = new TmxLanguageCollection();
            public TmxLanguageCollection ExportedLanguages = new TmxLanguageCollection();
        }

        public class Response
        {
            public TmxImage Result;
            public IList<string> Log = new List<string>();
        }

        public static Response Export(Args args)
        {
            Response ret = new Response();
            ret.Result = new TmxImage();
            ret.Result.Version = args.Original.Version;
            ret.Result.Header = new TmxHeader(args.Original.Header.DefaultCultureName);
            ret.Result.Header.ContextSeparator = args.Original.Header.ContextSeparator;
            ret.Result.Header.Cultures = args.ExportedLanguages.Clone();
            ret.Result.Items = new TmxItemCollection();

            ExcludeContextCollectionSnapshot excludeSnapshot =
                args.ExcludeList == null
                    ? ExcludeContextCollectionSnapshot.Empty 
                    : new ExcludeContextCollectionSnapshot(args.ExcludeList);

            foreach(TmxItem item in args.Original.Items)
            {
                bool isExcluded = excludeSnapshot.IsExcluded(item.Context);
                if (isExcluded)
                {
                    ret.Log.Add(string.Format("Excluded: {0}", item.Context));
                }
                else
                {
                    StringBuilder emptyLanguages = new StringBuilder();
                    if (args.RequiredLanguages != null)
                    {
                        foreach(TmxLanguage rl in args.RequiredLanguages)
                        {
                            string va = item.Values[rl.CultureName];
                            if (va == null || va.Length == 0)
                                emptyLanguages.Append(emptyLanguages.Length > 0 ? "," : "").Append(rl.CultureName);
                        }
                    }

                    if (emptyLanguages.Length > 0)
                    {
                        ret.Log.Add(string.Format("Empty {0}: {1}", emptyLanguages, item.Context));
                    }

                    else
                    {
                        TmxItem newItem = new TmxItem(item.Context);
                        foreach (TmxLanguage el in args.ExportedLanguages)
                        {
                            newItem.Values[el.CultureName] = item.Values[el.CultureName];
                            TmxItemValue
                                ivNew = newItem.Values.Get(el.CultureName),
                                iv = item.Values.Get(el.CultureName);

                            if (iv != null && ivNew != null)
                                ivNew.Ref = iv.Ref;
                        }

                        ret.Result.Items.Add(newItem);
                    }
                }
            }

            ret.Result.Items.Sort();
            return ret;
        }

    }
}
